package com.me.mysqladmin.service;

import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.me.mysqladmin.dao.DatabaseDAO;
import com.me.mysqladmin.dao.TableStatusDAO;
import com.me.mysqladmin.entity.DataBase;
import com.me.mysqladmin.util.MapUtil;

/**
 * 数据库业务层
 * @author dong
 */

@Service("dataBaseService")
public class DataBaseService {
	// 注入持久层对象
	@Resource
	private TableStatusDAO tableStatusDAO;
	@Resource
	private DatabaseDAO databaseDAO;
	
	/**
	 * 查询每个数据库对应的所有表
	 */
	public Map<String, List<String>> queryDbTree(){
		Map<String, List<String>> mapList = new LinkedHashMap<>();
		// 查询出所有可见数据库名称
		List<String> dbNames = queryVisibleNames();
		for(String dbName : dbNames){
			mapList.put(dbName, tableStatusDAO.queryTableNames(dbName));
		}
		return mapList;
	}
	
	/**
	 * 查询所有的可见数据库
	 */
	public List<DataBase> queryVisible(){
		List<DataBase> databases = databaseDAO.queryAll();
		// 从集合中删除information_schema，performance_schema，mysql这三个系统数据库
		for(int i = 0; i < databases.size(); i++){
			String dbName = databases.get(i).getSchema_name();
			if("information_schema".equals(dbName)
				|| "performance_schema".equals(dbName)
				|| "mysql".equals(dbName)){
				databases.remove(i);
			}
		}
		return databases;
	}
	
	/**
	 * 查询所有的可见数据库（全部字段）
	 */
	public List<Map<String, Object>> queryVisibleMap(){
		List<Map<String, Object>> databases = databaseDAO.queryAllMap();
		// 从集合中删除information_schema，performance_schema，mysql这三个系统数据库
		for(int i = 0; i < databases.size(); i++){
			String dbName = databases.get(i).get("schema_name").toString();
			if("information_schema".equalsIgnoreCase(dbName)
				|| "performance_schema".equalsIgnoreCase(dbName)
				|| "mysql".equalsIgnoreCase(dbName)){
				databases.remove(i);
			}
		}
		return databases;
	}
	
	/**
	 * 查询所有的可见数据库（指定字段）
	 */
	public List<Map<String, Object>> showDatabases(){
		List<Map<String, Object>> databases = queryVisibleMap();
		for(Map<String, Object> map : databases){
			MapUtil.simplify(map, DataBase.class);
		}
		return databases;
	}
	
	/**
	 * 查询所有的可见数据库名称
	 */
	public List<String> queryVisibleNames(){
		List<String> names = databaseDAO.queryNames();
		// 从集合中删除information_schema，performance_schema，mysql这三个系统数据库
		for(int i = 0; i < names.size(); i++){
			String dbName = names.get(i);
			if("information_schema".equalsIgnoreCase(dbName)
				|| "performance_schema".equalsIgnoreCase(dbName)
				|| "mysql".equalsIgnoreCase(dbName)){
				names.remove(i);
			}
		}
		return names;
	}
	
	/**
	 * 测试数据库连接
	 */
	public boolean testConnection(){
		return databaseDAO.isActive();
	}
	
	/**
	 * 创建数据库
	 */
	public Integer createDatabase(DataBase database) throws SQLException{
		Assert.notNull(database);
		Assert.hasText(database.getSchema_name());
		return databaseDAO.create(database.getSchema_name());
	}
	
	/**
	 * 删除数据库
	 */
	public Integer deleteDatabase(DataBase database) throws SQLException{
		Assert.notNull(database);
		Assert.hasText(database.getSchema_name());
		return databaseDAO.drop(database.getSchema_name());
	}
}
